﻿using System.IO;
using UnityEditor;
using UnityEngine;

namespace SocketWeaver.FixedMath.CustomUnityEditor
{
    public static class FMathMenu
    {
        [MenuItem("GameObject/SocketWeaver/FixedMath/FTransformInterpolationManager", false, 0)]
        public static void CreatePhysicsController2D()
        {
            var gameObject = new GameObject("FTransformInterpolationManager",
                typeof(FTransformInterpolationManager));

            Selection.activeGameObject = gameObject;
            SceneView.FrameLastActiveSceneView();
        }
    }

    [InitializeOnLoad]
    static class InstallGizmos
    {
        public static void Copy(string sourceDirectory, string targetDirectory)
        {
            DirectoryInfo diSource = new DirectoryInfo(sourceDirectory);
            DirectoryInfo diTarget = new DirectoryInfo(targetDirectory);

            CopyAll(diSource, diTarget);
        }

        public static void CopyAll(DirectoryInfo source, DirectoryInfo target)
        {
            Directory.CreateDirectory(target.FullName);

            // Copy each file into the new directory.
            foreach (FileInfo fi in source.GetFiles())
            {
                if(fi.Name.Contains("meta"))
                {
                    continue;
                }

                string path = Path.Combine(target.FullName, fi.Name);

                //only copy png files
                if (fi.Name.Contains(".png"))
                {
                    Debug.Log($"Copying src={fi.Name}");
                    Debug.Log($"Copying dst={path}");
                    fi.CopyTo(path, true);
                }
            }
        }

        public static void test()
        {
            string dstFile = Application.dataPath + "/Gizmos";

            if (!Directory.Exists(dstFile))
                Directory.CreateDirectory(dstFile);

            dstFile = Application.dataPath + "/Gizmos/Parallel";

            bool mustUpdate = false;
            if (!Directory.Exists(dstFile))
            {
                mustUpdate = true;
                Directory.CreateDirectory(dstFile);
            }

            string kPackageRoot = "Packages/com.socketweaver.parallel";
            string path = Path.GetFullPath(kPackageRoot + "/Gizmos/Parallel");
            path = path.Replace('\\', '/'); // because of GetFullPath()
            int index = path.LastIndexOf("/Editor");
            if (index >= 0)
                path = path.Substring(0, index);
            if (path.Length > 0)
                path = Path.GetFullPath(path);  // stupid backslashes

            //Debug.Log(path);

            var dstTime = Directory.GetCreationTime(dstFile);
            var srcTime = Directory.GetCreationTime(path);
            //Debug.Log($"dstTime={dstTime}");
            //Debug.Log($"srcTime={srcTime}");
            if (true)//srcTime > dstTime || mustUpdate)
            {
                Copy(path, dstFile);
                Debug.Log("Parallel icons updated");

            }
            else
            {
                Debug.Log("icons update-to-date");
            }
        }

        static InstallGizmos()
        {
            string dstFile = Application.dataPath + "/Gizmos";

            if (!Directory.Exists(dstFile))
                Directory.CreateDirectory(dstFile);

            dstFile = Application.dataPath + "/Gizmos/SocketWeaver";

            if (!Directory.Exists(dstFile))
                Directory.CreateDirectory(dstFile);

            dstFile = Application.dataPath + "/Gizmos/SocketWeaver/FixedMath";

            if (Directory.Exists(dstFile))
            {
                return;
            }

            Directory.CreateDirectory(dstFile);

            string kPackageRoot = "Packages/com.socketweaver.fixedmath";
            string path = Path.GetFullPath(kPackageRoot + "/Gizmos/FixedMath");
            path = path.Replace('\\', '/'); // because of GetFullPath()
            int index = path.LastIndexOf("/Editor");
            if (index >= 0)
                path = path.Substring(0, index);
            if (path.Length > 0)
                path = Path.GetFullPath(path);  // stupid backslashes

            Copy(path, dstFile);
            //Debug.Log(path);

            //var dstTime = Directory.GetCreationTime(dstFile);
            //var srcTime = Directory.GetCreationTime(path);
            //Debug.Log($"dstTime={dstTime}");
            //Debug.Log($"srcTime={srcTime}");
            //Debug.Log($"mustUpdate={mustUpdate}");
            //if (mustUpdate)
            //{
            //    Copy(path, dstFile);
            //}
            //else
            //{
            //    //Debug.Log("icons update-to-date");
            //}

        }
    }
}